package com.demo.apparel.service.uniapp.Impl;

import com.demo.apparel.entity.VisitCount;
import com.demo.apparel.entity.VisitCountDaily;
import com.demo.apparel.mapper.uniapp.VisitCountMapper;
import com.demo.apparel.service.uniapp.VisitCountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

@Service
public class VisitCountServiceImpl implements VisitCountService {

    @Autowired
    private VisitCountMapper visitCountMapper;

    @Override
    public void recordVisit(String uri) {
        VisitCount visitCount = visitCountMapper.getByUri(uri);
        if (visitCount == null) {
            VisitCount newVisit = new VisitCount();
            newVisit.setUri(uri);
            newVisit.setCount(1L);
            visitCountMapper.insert(newVisit);
        } else {
            visitCountMapper.incrementCount(uri);
        }
    }

    @Override
    public long getVisitCount(String uri) {
        VisitCount visitCount = visitCountMapper.getByUri(uri);
        return visitCount.getCount();
    }

    @Override
    public void recordVisitDaily(String uri) {
        visitCountMapper.insertOrUpdate(uri, LocalDate.now());
    }

    @Override
    public List<VisitCountDaily> getVisitsByToday() {
        return visitCountMapper.findByDate(LocalDate.now());
    }
}
